//
//  XWUITableViewExtension.swift
//  XiaoweHearingAid
//
//  Created by edy on 2024/11/27.
//

import Foundation
import UIKit

public extension UITableView {
    
    func scrollContentToBottom(animated: Bool) {
//        let section = self.numberOfSections - 1
//        if section < 0 {
//            return
//        }
//        
//        let row = self.numberOfRows(inSection: section) - 1
//        if row < 0 {
//            return
//        }
//        let indexPath = IndexPath(row: row, section: section)
//        self.scrollToRow(at: indexPath, at: .bottom, animated: animated)
        let section = self.numberOfSections - 1
        let row = self.numberOfRows(inSection: section) - 1
        self.scrollContentTo(section: section, row: row, scrollPosition: .bottom, animated: animated)
    }
    
    func scrollContentTo(section: Int, row: Int, scrollPosition: UITableView.ScrollPosition, animated: Bool) {
        if section < 0 || row < 0 {
            return
        }
        let maxSection = self.numberOfSections - 1
        if maxSection < 0 {
            return
        }
        let rSection = min(maxSection, section)
        let maxRow = self.numberOfRows(inSection: section) - 1
        if maxRow < 0 {
            return
        }
        let rRow = min(maxRow, row)
        let indexPath = IndexPath(row: rRow, section: rSection)
        self.scrollToRow(at: indexPath, at: scrollPosition, animated: animated)
    }
    
}
